﻿using JinfuyuanProject.Entity;
using JinfuyuanProject.Entity.Filter;
using JinfuyuanProject.Service;
using JinfuyuanProject.Service.Interface;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JinfuyuanProject.Controllers
{
    public class FinanceController : Controller
    {
        private readonly IEntryService _entryService;
        private readonly IStaffService _staffService;
        private readonly IPackageService _packageService;

        public FinanceController(IEntryService entryService, IStaffService staffService, IPackageService packageService)
        {
            _entryService = entryService;
            _staffService = staffService;
            _packageService = packageService;
        }

        // GET: Finance
        #region 页面
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Detail()
        {
            // 获取员工列表和套餐列表
            var staffs = _staffService.GetList().Where(s => s.Isdelete == 0).ToList();
            var packages = _packageService.GetList().Where(p => p.IsDelete == 0).ToList();

            ViewBag.Staffs = staffs;
            ViewBag.Packages = packages;

            return View();
        }

        public ActionResult FinanceList()
        {
            return View();
        }
        #endregion

        #region 接口
        public JsonResult DataList(int page = 1, int limit = 10, string startDate = null, string endDate = null, int? type = null, int? verify = null, string contractNumber = null)
        {
            FinanceFilter filter = new FinanceFilter
            {
                page = page,
                limit = limit,
                StartDate = startDate,
                EndDate = endDate,
                Type = type,    //筛选出入帐
                Verify = verify,  //筛选状态
                ContractNumber = contractNumber   //筛选合同编号
            };

            Hashtable ht = _entryService.ListData(filter);
            return Json(ht, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult ConfirmFinance(int id)
        {
            var entry = _entryService.GetModel(id);
            if (entry == null)
            {
                return Json(new { code = -1, msg = "记录不存在" });
            }
            Staff staff= HttpContext.Session["user"] as Staff;
            // 更新审核状态（1=已审核）
            entry.Verify = 1;
            entry.VerifyId = staff.Id; // 这里应该用当前登录用户ID，暂时写死为1

            bool success = _entryService.Update(entry);
            return Json(new { code = success ? 0 : -1, msg = success ? "审核成功" : "审核失败" });
        }

        [HttpPost]
        public JsonResult DataInsert(Entry entry)
        {
            // 设置默认值
            Staff staff = HttpContext.Session["user"] as Staff;
            entry.CreateTime = DateTime.Now;
            entry.FounderId = staff.Id; // 获取当前登录用户ID
            entry.Verify = 0; // 默认未审核
            entry.IsDelete = 0; // 默认未删除

            int id = _entryService.Insert(entry);
            return Json(new { code = id > 0 ? 0 : -1, msg = id > 0 ? "添加成功" : "添加失败" });
        }
        #endregion
    }
}
